package dormouse.vfs.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.jdesktop.swingx.JXFrame;

import dormouse.vfs.ui.actions.BackAction;
import dormouse.vfs.ui.actions.CopyFileAction;
import dormouse.vfs.ui.actions.CutFileAction;
import dormouse.vfs.ui.actions.DeleteFileAction;
import dormouse.vfs.ui.actions.EmailFileAction;
import dormouse.vfs.ui.actions.FileInfoAction;
import dormouse.vfs.ui.actions.FwdAction;
import dormouse.vfs.ui.actions.NewFileAction;
import dormouse.vfs.ui.actions.OpenFileAction;
import dormouse.vfs.ui.actions.PasteFileAction;
import dormouse.vfs.ui.actions.RootAction;
import dormouse.vfs.ui.actions.SyncFileAction;
import dormouse.vfs.ui.actions.UpAction;


/**
 * @author Mark
 *
 */
public class FileBrowser extends JXFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4306839766358767310L;
	FileTreeTable canvas = null;
	Dimension pref = new Dimension(400, 300);  //  @jve:decl-index=0:
	private JMenuBar jJMenuBar = null;
	private JToolBar jToolBarBar = null;
	private JButton backBtn = null;  //  @jve:decl-index=0:visual-constraint="139,414"
	private JButton fwdBtn = null;  //  @jve:decl-index=0:visual-constraint="299,414"
	private JButton reloadBtn = null;  //  @jve:decl-index=0:visual-constraint="283,410"
	private JButton homeBtn = null;  //  @jve:decl-index=0:visual-constraint="268,409"
	private JButton rootBtn = null;  //  @jve:decl-index=0:visual-constraint="436,416"
	private JButton upBtn = null;  //  @jve:decl-index=0:visual-constraint="403,409"
	private JPanel toolbarPanel = null;
	private JToolBar locationBar = null;
	private JLabel locationLabel = null;
	private JTextField locationFld = null;
	private JComboBox viewCombo = null;
	
	//private HomeAction homeAction = new HomeAction(this);
	private RootAction rootAction = null;
	private BackAction backAction = null;
	private FwdAction fwdAction = null;
	private UpAction upAction = null;
	private NewFileAction newFileAction = new NewFileAction();
	private FileInfoAction fileInfoAction = new FileInfoAction();
	private EmailFileAction emailFileAction = new EmailFileAction();
	private OpenFileAction openFileAction = new OpenFileAction();
	private PasteFileAction pasteFileAction = null;
	private CutFileAction cutFileAction = new CutFileAction();
	private CopyFileAction copyFileAction = new CopyFileAction();
	//private AddBookmarkAction bookmarkAction = new AddBookmarkAction();
	private DeleteFileAction deleteFileAction = new DeleteFileAction();
	private SyncFileAction syncFileAction = new SyncFileAction();
	
	
	private Stack backStk = new Stack();
	private Stack fwdStk = new Stack();
	
	
	
	public Stack getBackStk() {
		return backStk;
	}



	public void setBackStk(Stack backStk) {
		this.backStk = backStk;
	}



	public Stack getFwdStk() {
		return fwdStk;
	}



	public void setFwdStk(Stack fwdStk) {
		this.fwdStk = fwdStk;
	}



	public FileBrowser(){
		initialize();
	}
		
    
	
	public void addTreeSelectionListener(TreeSelectionListener tsl){
		canvas.addTreeSelectionListener(tsl);
	}
	
	public void setDirectory(FileObject file){
		canvas.setDirectory(file);
		this.locationFld.setText(file.getName().getURI());
	}
 

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		  try {
			canvas = new FileTreeTable();
		} catch (FileSystemException e) {
			e.printStackTrace();
		}
		canvas.addTreeSelectionListener(new TreeSelectionListener(){

			public void valueChanged(TreeSelectionEvent tse) {
				FileNode node = (FileNode)canvas.fileTree.getLastSelectedPathComponent();
				if (node == null) return;
				locationFld.setText(node.getFile().getName().getURI());
			}});
          this.setLayout(new BorderLayout());
          this.setJMenuBar(getJJMenuBar());
          this.setResizable(true);
          
          
          this.add(getToolbarPanel(), BorderLayout.NORTH);
          this.add(canvas, BorderLayout.CENTER);
          this.setPreferredSize(new Dimension(653, 374));
          this.setTitle("Ammonite");
          this.pack();
	}




	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			JMenu fileMenu = new JMenu("File");
			fileMenu.add(newFileAction);
			fileMenu.add(openFileAction);
			fileMenu.add(fileInfoAction);
			fileMenu.add(emailFileAction);
			fileMenu.add(syncFileAction);
			
			JMenu editMenu = new JMenu("Edit");
			editMenu.add(cutFileAction);
			editMenu.add(copyFileAction);
			//editMenu.add(pasteFileAction);
			editMenu.add(deleteFileAction);
			
			JMenu viewMenu = new JMenu("View");
			
			JMenu placesMenu = new JMenu("Places");
			//placesMenu.add(rootAction);
			//placesMenu.add(homeAction);
			placesMenu.addSeparator();
			//placesMenu.add(backAction);
			//placesMenu.add(fwdAction);
			//placesMenu.add(upAction);
			
			JMenu helpMenu = new JMenu("Help");
			
			jJMenuBar.add(fileMenu);
			jJMenuBar.add(editMenu);
			jJMenuBar.add(viewMenu);
			jJMenuBar.add(placesMenu);
			jJMenuBar.add(helpMenu);
		}
		return jJMenuBar;
	}
	
	private JPanel getToolbarPanel(){
		if (toolbarPanel == null){
	        toolbarPanel = new JPanel();
	        toolbarPanel.setLayout(new FlowLayout());
	        toolbarPanel.add(getJToolBarBar());
	        toolbarPanel.add(getLocationBar());
	        toolbarPanel.setPreferredSize(new Dimension(400, 40));
		}
		return toolbarPanel;
	}




	/**
	 * This method initializes jToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getJToolBarBar() {
		if (jToolBarBar == null) {
			jToolBarBar = new JToolBar();
			jToolBarBar.add(getBackBtn());
			jToolBarBar.add(getFwdBtn());
			jToolBarBar.add(getUpBtn());
			jToolBarBar.add(getReloadBtn());
			jToolBarBar.addSeparator();
			jToolBarBar.add(getHomeBtn());
			jToolBarBar.add(getRootBtn());
			jToolBarBar.addSeparator();
		}
		return jToolBarBar;
	}




	/**
	 * This method initializes backBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBackBtn() {
		if (backBtn == null) {
			//backBtn = new JButton(backAction);
		}
		return backBtn;
	}




	/**
	 * This method initializes fwdBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getFwdBtn() {
		if (fwdBtn == null) {
			//fwdBtn = new JButton(fwdAction);
		}
		return fwdBtn;
	}




	/**
	 * This method initializes reloadBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getReloadBtn() {
		if (reloadBtn == null) {
			//reloadBtn = new JButton(reloadAction);
		}
		return reloadBtn;
	}




	/**
	 * This method initializes homeBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getHomeBtn() {
		if (homeBtn == null) {
			//homeBtn = new JButton(new HomeAction(this));
		}
		return homeBtn;
	}




	/**
	 * This method initializes rootBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRootBtn() {
		if (rootBtn == null) {
			//rootBtn = new JButton(new RootAction(this));
		}
		return rootBtn;
	}




	/**
	 * This method initializes upBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUpBtn() {
		if (upBtn == null) {
			//upBtn = new JButton(new UpAction(this));
		}
		return upBtn;
	}




	/**
	 * This method initializes locationBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getLocationBar() {
		if (locationBar == null) {
			locationLabel = new JLabel("Location:");
			
			locationBar = new JToolBar();
			locationBar.add(locationLabel);
			locationBar.add(getLocationFld());
			locationBar.add(getViewCombo());
		}
		return locationBar;
	}




	/**
	 * This method initializes locationFld	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLocationFld() {
		if (locationFld == null) {
			locationFld = new JTextField();
			locationFld.setPreferredSize(new Dimension(100, 20));
			locationFld.setFocusable(true);
		}
		return locationFld;
	}




	/**
	 * This method initializes viewCombo	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getViewCombo() {
		if (viewCombo == null) {
			viewCombo = new JComboBox(canvas.getRendererNameList().toArray());
			
		}
		return viewCombo;
	}




	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		LookAndFeel lf = UIManager.getLookAndFeel();
	    
	    // Install a different look and feel; specifically, the Windows look and feel
	    try {
	        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } catch (InstantiationException e) {
	    } catch (ClassNotFoundException e) {
	    } catch (UnsupportedLookAndFeelException e) {
	    } catch (IllegalAccessException e) {
	    }
		FileBrowser browser = new FileBrowser();
		browser.setVisible(true);
		browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}  //  @jve:decl-index=0:visual-constraint="42,11"
